package com.weiweiqin.controller.home;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpSession;

import com.weiweiqin.model.Color;
import com.weiweiqin.model.ColorSizeItem;
import com.weiweiqin.model.Size;
import com.weiweiqin.service.ColorService;
import com.weiweiqin.service.ColorSizeItemService;
import com.weiweiqin.service.SizeService;
import com.weiweiqin.utils.BigDemicalUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.weiweiqin.model.Goods;
import com.weiweiqin.service.GoodsService;
import com.weiweiqin.vo.common.CartVO;
import com.weiweiqin.vo.common.Result;

@Controller
@RequestMapping("front/cart")
public class CartController {

	@Autowired
	private GoodsService goodsService;

	@Autowired
	private ColorService colorService;

	@Autowired
	private ColorSizeItemService colorSizeItemService;

	@Autowired
	private SizeService sizeService;

	private static final Logger logger = LoggerFactory
			.getLogger(CartController.class);

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addCart.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String addCart(Integer itemId,Integer goodsId, int num, Integer colorId,Integer sizeId,HttpSession session) {
		logger.info("Enter method addCart,goodsId=" + goodsId + ":num=" + num+":colorId="+colorId+":sizeId="+sizeId);
		Result result = new Result();
		//以商品ID、颜色、尺码为key
		Color color = colorService.get(colorId);
		Size size = sizeService.get(sizeId);
		String key = genSessionKey(goodsId,color.getColor(),size.getSize());
		Map<String, CartVO> carts = null;
		if (null != session.getAttribute("carts")) {
			carts = (Map<String, CartVO>) session.getAttribute("carts");
		} else {
			carts = new HashMap<String, CartVO>();
		}
		if (carts.containsKey(key)) {
			CartVO cartVO = carts.get(key);
			cartVO.setNum(cartVO.getNum()+num);
		} else {
			CartVO cartVO = new CartVO();
			cartVO.setColor(color.getColor());
			cartVO.setSize(size.getSize());
			cartVO.setNum(num);
			cartVO.setItemId(itemId);
			cartVO.setGoodsId(goodsId);
			cartVO.setPic(color.getPic());
			carts.put(key,cartVO);
			//设置最新商品
			session.setAttribute("lastCarts", cartVO);
		}
		session.setAttribute("carts", carts);
		
		result.setResult(true);
		return result.toJson();
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delCart.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String delCart(int goodsId, String color,String size,HttpSession session) {
		Result result = new Result();
		if(null == color){
			color = "";
		}
		if(null == size){
			size = "";
		}
		String key = genSessionKey(goodsId,color,size);
		Map<String, Integer> carts = null;
		if (null != session.getAttribute("carts")) {
			carts = (Map<String, Integer>) session.getAttribute("carts");
		} else {
			carts = new HashMap<String, Integer>();
		}
		if (carts.containsKey(key)) {
			carts.remove(key);
		}
		result.setResult(true);
		return result.toJson();
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getCartList.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String getCartList(HttpSession session) {
		Result result = new Result();
		Map<String, CartVO> carts = null;
		List<CartVO> cartList = new ArrayList<CartVO>();
		if (null != session.getAttribute("carts")) {
			carts = (Map<String, CartVO>) session.getAttribute("carts");
			Set<Entry<String, CartVO>> entrySet = carts.entrySet();
			for (Entry<String, CartVO> entry : entrySet) {
				CartVO vo = entry.getValue();
				Goods goods = goodsService.get(vo.getGoodsId());
				ColorSizeItem colorSizeItem = colorSizeItemService.get(vo.getItemId());
				vo.setGoods(goods);
				vo.setColorSizeItem(colorSizeItem);
				cartList.add(vo);
			}
		} 
		result.setObj(cartList); 
		result.setResult(true);
		return result.toJson();
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getCartListByKeys.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String getCartListByKeys(String keys,HttpSession session) {
		String[] keyStrs = keys.split(",");
		Result result = new Result();
		Map<String, CartVO> carts = null;
		List<CartVO> cartList = new ArrayList<CartVO>();
		double totalPrice = 0.00;
		if (null != session.getAttribute("carts")) {
			carts = (Map<String, CartVO>) session.getAttribute("carts");
			for(int i =0;i<keyStrs.length;i++){
				String keyStr = keyStrs[i];
				CartVO cartVO = carts.get(keyStr);
				Goods goods = goodsService.get(cartVO.getGoodsId());
				ColorSizeItem colorSizeItem = colorSizeItemService.get(cartVO.getItemId());
				cartVO.setColorSizeItem(colorSizeItem);
				totalPrice = totalPrice + BigDemicalUtil.mul(colorSizeItem.getPrice(), cartVO.getNum()*100,2);
				cartVO.setGoods(goods);
				cartList.add(cartVO);
			}
		}
		Map<String,Object> mp = new HashMap<String,Object>();
		mp.put("cartList",cartList);
		mp.put("totalPrice",totalPrice);
		result.setObj(mp);
		result.setResult(true);
		return result.toJson();
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getCartNums.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String getCartNums(HttpSession session) {
		Result result = new Result();
		Map<String, CartVO> carts = null;
		int num = 0;
		if (null != session.getAttribute("carts")) {
			carts = (Map<String, CartVO>) session.getAttribute("carts");
			Set<Entry<String, CartVO>> entrySet = carts.entrySet();
			num = entrySet.size();
		}
		result.setObj(num);
		result.setResult(true);
		return result.toJson();
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getCartNum.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String getCartNum(HttpSession session) {
		int num = 0;
		if (null != session.getAttribute("carts")) {
			Map<String, CartVO> carts  = (Map<String, CartVO>) session.getAttribute("carts");
			num = carts.entrySet().size();
		} 
		return num+"";
	}
	
	@RequestMapping(value = "/modifyNum.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	private String modifyNum(String key,int num,HttpSession session){
		Result result = new Result();
		try {
			if (null != session.getAttribute("carts")) {
				@SuppressWarnings("unchecked")
				Map<String, CartVO> carts = (Map<String, CartVO>) session
						.getAttribute("carts");
				CartVO cartVO = carts.get(key);
				cartVO.setNum(num);
			}
		} catch (Exception e) {
			logger.error("修改购物车商品数量异常...",e);
			result.setResult(false);
		}
		result.setResult(true);
		return result.toJson();
	}
	
	
	private String  genSessionKey(int goodsId,String color,String size){
		
		return goodsId+"_"+color+"_"+size;
	}

	public GoodsService getGoodsService() {
		return goodsService;
	}

	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}

}
